#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QPlainTextEdit>
#include <QTabWidget>
#include "my_label.h"
#include "my_group.h"
#include "my_double.h"
#include "my_spin.h"
#include "my_combo.h"
#include "my_spin_group.h"
#include "my_combo_send.h"
#include "my_lcd.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include "my_triaxial.h"
#include "my_switch.h"
#include "my_switch2.h"
#include <QRadioButton>
#include <QTableView>
#include <QDebug>
#include <QTcpSocket>
#include "protocol.h"
#include "QTimer"
#include <QStandardItemModel>
#include <QDateTime>
#include <QFileDialog>
#include "my_spin_btn_group.h"
#include "my_update.h"
#include <QThread>
#include <my_update_thread.h>

class Widget : public QWidget
{
    Q_OBJECT

public:
    //公有宏
    #define software_version "1.2"    //版本
    #define BYTE0(dwTemp) ( *( (char*)(&dwTemp)    ) )
    #define BYTE1(dwTemp) ( *( (char*)(&dwTemp) + 1) )
    #define BYTE2(dwTemp) ( *( (char*)(&dwTemp) + 2) )
    #define BYTE3(dwTemp) ( *( (char*)(&dwTemp) + 3) )

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();

    void init_data();
    void init_window();
    void init_connect();
    void init_layout();
    void write_log(QString str);    //写日志
    void set_btn_enable(bool flag);
    void status_clear();
    void switch_send(uint8_t type, uint8_t control);
    void steer_send(uint8_t control);
    void note_battery(float voltage, float current, float present, float leave_power);

public slots:
    // 特殊槽
    void slot_socket_read_data();
    void slot_connect();
    void slot_disconnect();
    void slot_send();
    void slot_timeout();
    void slot_set_log_path();
    void slot_comboBox(QString str);
    void slot_send_date(QByteArray date, int percent);
    void slot_erase_flash();
    void slot_update_successful();

    // 按键槽
    void slot_pushButton_send_ackerman();
    void slot_pushButton_send_ackerman_stop();
    void slot_pushButton_send_fine();
    void slot_pushButton_send_fine_stop();
    void slot_pushButton_reset_odom();
    void slot_pushButton_send_mode();
    void slot_pushButton_send_cmd();
    void slot_set_light();
    void slot_version();
    void slot_update();
    void slot_getfile();

signals:
    void signal_go_update(QString filepath);

private:
    void send(QByteArray data);     //发送函数，封装一下
    void display_text(QString text);    //打印窗口打印
    void display_text(QByteArray SendData, int direction);  //打印byte
    int  char2int(char ch);
    void update_ui();
    QString get_wheel_state(int num);

private:
    // 核心
    QTcpSocket *socket;         //网络指针
    protocol *protocol_value;   // 协议解析
    QTimer * timer;             // 计时器

    // 界面
    QPlainTextEdit* edit;

    QTabWidget* tab;
    QWidget* tab_page01;
    QWidget* tab_page02;
    QWidget* tab_page03;
    QWidget* tab_page04;
    QWidget* tab_page05;

    // 超声波
    My_group* ult;
    // 触边
    My_double* edge;
    // 轮子状态
    QGroupBox* wheel_state;
    My_group* line_state;
    My_group* turn_state;

    // 遥控器
    My_double* mc6c;
    // 轮子编码器
    QGroupBox* wheel_encoder;
    My_group* line_v;
    My_group* turn_deg;
    My_group* encoder;

    // 里程计
    QGroupBox* odom;
    My_double* distance;
    My_double* speed;
    My_double* degree;
    My_double* flag;

    // 里程计清零
    QGroupBox* odom_clear;
    My_spin* odom_clear_x;
    My_spin* odom_clear_y;
    My_spin* odom_clear_yaw;
    QPushButton* btn_reset_odom;

    // 阿克曼控制
    QGroupBox* ack_control;
    My_Combo* ack_mode;
    My_spin* ack_v;
    My_spin* ack_w;
    My_spin* ack_r;
    My_spin* ack_deg;
    QPushButton* btn_ack_speed;
    QPushButton* btn_ack_continue;
    QPushButton* btn_ack_stop;

    //精细控制
    QGroupBox* fine_control;
    My_spin_group* fine_speed;
    My_spin_group* fine_degree;
    QPushButton* btn_fine_speed;
    QPushButton* btn_fine_continue;
    QPushButton* btn_fine_stop;

    //运动模式
    My_combo_send* motion_mode;
    My_combo_send* cmd;
    My_switch2* steer;
    My_spin_btn_group* calibration;
    QPushButton* btn_emergency_stop;

    // IMU控件
    My_triaxial* imu_degree;
    My_triaxial* imu_acc;
    My_triaxial* imu_gyro;
    My_triaxial* imu_magic;

    // 电池
    QGroupBox* battery;
    My_label* charge_flag;
    My_label* current;
    My_label* voltage;
    My_label* remain_power;
    My_label* sum_power;
    My_label* percent;

    // 开关按键
    const QStringList list_sw_name = {"总电源开关", "充电继电器", "急停按钮",\
                                      "导航激光雷达", "二维码相机", "舵机", \
                                      "lora", "丝杆", "机械臂", "语音对讲",\
                                     "拾音器", "导航工控机", "图像工控机", "无线网桥"};
    QList<My_switch*> list_sw;

    QList<My_switch*> list_relay;
    #define RELAY_NUM 8

    QLCDNumber* lcd_light;
    QPushButton* btn_light;
    qint8 light_flag{0};

    QPushButton* btn_set_path;
    QLineEdit* line_log;
    QString logpath{"E:\01 智能小车项目\08电池"};
    QRadioButton* radio_log;

    // 雷达控件
    QGroupBox* group_radar_front;
    QGroupBox* group_radar_back;
    QTableView* radar_front;
    QTableView* radar_back;
    QStandardItemModel *table_mode_f;
    QStandardItemModel *table_mode_b;

    // 升级控件
    My_update* update_wig;
    QThread* thread_01;
    My_update_thread *update_thread;
    QString filepath = "C:/rtthread.rbl";

    // 接收频率
    QGroupBox* lcd_group;
    My_lcd* lcd_dipan_heart;
    My_lcd* lcd_ult;
    My_lcd* lcd_other;
    My_lcd* lcd_encoder;
    My_lcd* lcd_speed;
    My_lcd* lcd_dipan_reply;
    My_lcd* lcd_imu;
    My_lcd* lcd_dipan_status;

    My_lcd* lcd_dianyuan_heart;
    My_lcd* lcd_battery;
    My_lcd* lcd_dianyuan_status;
    My_lcd* lcd_dianyuan_reply;
    My_lcd* lcd_radar;

    // 底部控键
    QLabel* lab_ip;
    QLineEdit* line_ip;
    QLabel* lab_port;
    QLineEdit* line_port;
    QComboBox* combo_tcpip;
    QPushButton* btn_connect;

    QLineEdit* line_send;
    QPushButton* btn_send;
    QPushButton* btn_clear;

    // const 变量
    const QStringList strlist_IP = {"底盘控制器", "电源板9000", "电源板9001", "自测"};
    const QStringList strlist_ack_mode = {"原地转弯", "四轮阿克曼模式", "四轮调整朝向", "斜行模式", "横向行驶", "两轮阿克曼模式", "两轮调整朝向", "差速模式"};
    const QStringList strlist_motion_mode = {"四轮阿克曼", "两轮阿克曼", "差速"};
    const QStringList strlist_cmd = {"清除错误", "轮子朝向角归零", "防撞条清零"};
    const QStringList strlist_radar = {"ID", "纵向距离", "横向距离", "纵向速度", "横向速度"};
    const int timer_frequence{200};
    const float log_frequence{0.2};
};
#endif // WIDGET_H
